﻿package mortal.game.view.closedBetaGiftBag
{
    import Framework.MQ.*;
    import Message.Game.*;
    import Message.Public.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.closedBetaGiftBagNew.*;
    import mortal.game.view.closedBetaGiftBagNew.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ClosedBetaGiftBagController extends Controller
    {
        private var _module:ClosedBetaGiftBagModule;
        private var _isShowIcon:Boolean = false;

        public function ClosedBetaGiftBagController()
        {
            return;
        }// end function

        private function ShowIcon() : void
        {
            if (ClosedBetaGiftBagUtil.CheckOpenLevel())
            {
                if (!ClosedBetaGiftBagIcon.hasInstance || ClosedBetaGiftBagIcon.instance.isHide)
                {
                    ClosedBetaGiftBagIcon.instance.show();
                }
                this._isShowIcon = true;
            }
            else
            {
                if (ClosedBetaGiftBagIcon.hasInstance && !ClosedBetaGiftBagIcon.instance.isHide)
                {
                    ClosedBetaGiftBagIcon.instance.hide();
                }
                this._isShowIcon = false;
            }
            return;
        }// end function

        private function onTopListReq(event:DataEvent = null) : void
        {
            GameProxy.ranking.getToplist(EToplistType._EToplistTypePlayerLevel);
            return;
        }// end function

        private function onGetReward(event:DataEvent) : void
        {
            GameProxy.role.getFinalTestReward(event.data as int);
            return;
        }// end function

        private function onClosedBetaGiftBagShow(event:DataEvent) : void
        {
            if (!isViewShow)
            {
                view.show();
                this._module.setDefaultPanel();
            }
            else
            {
                view.hide();
            }
            return;
        }// end function

        private function onRankingGetRankListSucc(param1:Object) : void
        {
            if (param1.toplistType == EToplistType._EToplistTypePlayerLevel)
            {
                if (this._module && this._module.curSelView && this._module.curSelView is ClosedBetaGiftBagOldView)
                {
                    (this._module.curSelView as ClosedBetaGiftBagOldView).updatePlayerList(param1.rankingList);
                }
                Cache.instance.closedBetaGiftBag.topRankInfo = param1;
            }
            return;
        }// end function

        private function onRoleLevelUpdate(param1:Object) : void
        {
            if (!this._isShowIcon)
            {
                this.ShowIcon();
            }
            if (cache.closedBetaGiftBag.hasReceived)
            {
                this.notifyWelfareIcon();
                if (isViewShow)
                {
                    if (this._module)
                    {
                        this._module.refresh();
                        if (this._module.curSelView)
                        {
                            this._module.curSelView.refresh();
                        }
                    }
                }
            }
            return;
        }// end function

        private function onRewardInfo(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SFinalTestRewardInfo;
            cache.closedBetaGiftBag.isGetToday = _loc_2.isGetToday;
            cache.closedBetaGiftBag.rewardInfo = _loc_2.rewardInfo;
            if (!cache.closedBetaGiftBag.hasReceived && ClosedBetaGiftBagIcon.hasInstance && !ClosedBetaGiftBagIcon.instance.isHide)
            {
                ClosedBetaGiftBagIcon.instance.stopEffect();
            }
            else
            {
                ClosedBetaGiftBagIcon.instance.playEffect();
            }
            if (isViewShow)
            {
                this.onOldViewRefresh();
            }
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new ClosedBetaGiftBagModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            cache.closedBetaGiftBag.isShow = true;
            Dispatcher.addEventListener(EventName.ClosedBetaGiftBagTopListReq, this.onTopListReq);
            Dispatcher.addEventListener(EventName.ClosedBetaGiftBagGetReward, this.onGetReward);
            NetDispatcher.addCmdListener(ServerCommand.RankingGetRankListSucc, this.onRankingGetRankListSucc);
            Dispatcher.addEventListener(TimeEvent.DateChange, this.onDateChange);
            Dispatcher.addEventListener(EventName.ClosedBetaLevelGiftRequest, this.onClosedBetaLevelGiftRequest);
            Dispatcher.addEventListener(EventName.ClosedBetaTimingGiftRequest, this.onClosedBetaTimingGiftRequest);
            Dispatcher.addEventListener(EventName.ClosedBetaOnlineGiftRequest, this.onClosedBetaOnlineGiftRequest);
            return;
        }// end function

        private function onClosedBetaOnlineGiftNotify(event:Event) : void
        {
            this.updateFlag();
            this.notifyWelfareIcon();
            if (isViewShow)
            {
                if (this._module)
                {
                    this._module.refresh();
                    if (this._module.curSelView)
                    {
                        this._module.curSelView.refresh();
                    }
                }
            }
            return;
        }// end function

        private function onClosedBetaTimingGiftNotify(event:Event) : void
        {
            this.updateFlag();
            this.notifyWelfareIcon();
            if (isViewShow)
            {
                if (this._module)
                {
                    this._module.refresh();
                    if (this._module.curSelView)
                    {
                        this._module.curSelView.refresh();
                    }
                }
            }
            return;
        }// end function

        private function updateFlag() : void
        {
            var _loc_1:* = cache.closedBetaGiftBag.hasReceived;
            cache.closedBetaGiftBag.isFlash = _loc_1;
            return;
        }// end function

        private function onClosedBetaLevelGiftRequest(event:DataEvent) : void
        {
            GameProxy.closedBetaGiftBag.getLevelReward(event.data as int);
            return;
        }// end function

        private function onClosedBetaTimingGiftRequest(event:DataEvent) : void
        {
            GameProxy.closedBetaGiftBag.getTimingReward(event.data as int);
            return;
        }// end function

        private function onClosedBetaOnlineGiftRequest(event:DataEvent) : void
        {
            GameProxy.closedBetaGiftBag.getOnlineReward(event.data as int);
            return;
        }// end function

        private function onOldViewShow(event:Event) : void
        {
            this.onOldViewRefresh();
            return;
        }// end function

        private function onDateChange(event:TimeEvent) : void
        {
            this.onOldViewRefresh();
            return;
        }// end function

        private function onOldViewRefresh() : void
        {
            this.onTopListReq();
            if (this._module && this._module.curSelView && this._module.curSelView is ClosedBetaGiftBagOldView)
            {
                (this._module.curSelView as ClosedBetaGiftBagOldView).refresh();
            }
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            cache.closedBetaGiftBag.isShow = true;
            Dispatcher.removeEventListener(EventName.ClosedBetaGiftBagTopListReq, this.onTopListReq);
            Dispatcher.removeEventListener(EventName.ClosedBetaGiftBagGetReward, this.onGetReward);
            NetDispatcher.removeCmdListener(ServerCommand.RankingGetRankListSucc, this.onRankingGetRankListSucc);
            Dispatcher.removeEventListener(EventName.ClosedBetaLevelGiftRequest, this.onClosedBetaLevelGiftRequest);
            Dispatcher.removeEventListener(EventName.ClosedBetaTimingGiftRequest, this.onClosedBetaTimingGiftRequest);
            Dispatcher.removeEventListener(EventName.ClosedBetaOnlineGiftRequest, this.onClosedBetaOnlineGiftRequest);
            return;
        }// end function

        private function notifyWelfareIcon() : void
        {
            var _loc_1:* = cache.closedBetaGiftBag.hasReceived;
            if (_loc_1)
            {
                ClosedBetaGiftBagIcon.instance.playEffect();
            }
            else
            {
                ClosedBetaGiftBagIcon.instance.stopEffect();
            }
            return;
        }// end function

        private function onClosedBetaGiftInfoGet(param1:SAgencyRewardInfo) : void
        {
            cache.closedBetaLevelGift.levelGiftInfo = param1.levelInfos;
            cache.closedBetaOnlineGift.onlineGiftInfo = param1.onLineInfos;
            cache.closedBetaTimingGift.timingGiftInfo = param1.timingInfos;
            if (!this._isShowIcon)
            {
                this.ShowIcon();
            }
            this.notifyWelfareIcon();
            this.updateFlag();
            if (isViewShow)
            {
                if (this._module)
                {
                    this._module.refresh();
                    if (this._module.curSelView)
                    {
                        this._module.curSelView.refresh();
                    }
                }
            }
            return;
        }// end function

        private function onClosedBetaOnlineTimeUpdate(event:DataEvent) : void
        {
            if (cache.closedBetaGiftBag.hasReceived)
            {
                ClosedBetaGiftBagIcon.instance.playEffect();
            }
            else
            {
                ClosedBetaGiftBagIcon.instance.stopEffect();
            }
            if (isViewShow)
            {
                if (this._module)
                {
                    this._module.refresh();
                    if (this._module.curSelView)
                    {
                        this._module.curSelView.refresh();
                    }
                }
            }
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.ClosedBetaGiftBagShow, this.onClosedBetaGiftBagShow);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onRoleLevelUpdate);
            Dispatcher.addEventListener(EventName.ClosedBetaGiftBagOldViewShow, this.onOldViewShow);
            NetDispatcher.addCmdListener(ServerCommand.ClosedBetaGiftInfoGet, this.onClosedBetaGiftInfoGet);
            Dispatcher.addEventListener(EventName.LineOnInitData, this.onClosedBetaOnlineTimeUpdate);
            Dispatcher.addEventListener(EventName.ClosedBetaOnlineGiftNotify, this.onClosedBetaOnlineGiftNotify);
            Dispatcher.addEventListener(EventName.ClosedBetaTimingGiftNotify, this.onClosedBetaTimingGiftNotify);
            return;
        }// end function

    }
}
